#include <stdio.h>
#include "string.h"
#include "driver/gpio.h"
#include "My_Flash.h"
#include "MyUart.h"
#include "NandFlash.h"
#include "RTC_SD2506P.h"
#include "RJMU401.h"

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include "esp_log.h"

static const char *TAG = "Main";

#define ASW3401_SEL_N GPIO_NUM_37
#define ASW3401_OE_N  GPIO_NUM_36

void Test_RJMU401( int Is_Test) {
    esp_err_t err;
    if (Is_Test == 0) return;
     
    uint8_t Key[20] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,0}; 
    for(int i =0 ;i< 20; i++){
        printf("%d", Key[i]);
    }
    err = RJMU_Crypto_Init();
    if (err == ESP_OK) {
        printf("\nRJMU401 initial OK.\n");
    } else {
        printf("\nRJMU401 initial Failed.\n");
    }
    
   // RJMU401_Get_ID();
    Set_SM4_Key(Key , 16) ;

    uint8_t plaintext[7] = {'a' , 'b' ,'c','1','2','3','0'};
    uint8_t ciphertext[64]={0};
    uint16_t ciphertext_len = 64;

    uint8_t plaintext1[64] ;
    uint16_t plaintext_len1 = 64;

    int i;

    RJMU401_encrypt( plaintext, 7 ,  ciphertext , &ciphertext_len) ;

    printf("\nciphertext\n");
    for ( i = 0; i < ciphertext_len; i++) printf("%2x" , ciphertext[i]);

   //RJMU401_decrypt( ciphertext , ciphertext_len - 4 , plaintext1 , &plaintext_len1 ) ;
    RJMU401_decrypt( ciphertext , ciphertext_len - 4 , plaintext1 , &plaintext_len1 ) ;
    printf("\nplaintext: [%d]\n" ,plaintext_len1 );
    for ( i = 0; i < plaintext_len1; i++) printf("%c[%2x]" , plaintext1[i], plaintext1[i]);
     printf("\n\n");

}
/*************************/
void Test_Flash( int Is_Test) {
      if (Is_Test == 0) return;
       char write_key_value[100] = {0};
       char read_key_value[100]  , msg[200];

       size_t length;
   
       sprintf(write_key_value , "AAA[%s]BBB","12345");
       nvs_write("Key1" , write_key_value);
       nvs_read ("Key1" , read_key_value , &length) ;

       if (strcmp(write_key_value , read_key_value) == 0) {
           printf("\nTest to the Flash is OK!\n");
       }
}

/**************************************/
void Test_NandFlash( int Is_Test) {
 
    char s1[100]={0} , s2[100]={0};

    esp_err_t ret;
    size_t n;

    sprintf(s1 , "%s" , "iewuqyiuwqe12345ABCD");

    Nand_write_file( s1);
    Nand_read_file(s2);

     if (strcmp( s1 , s2) == 0) {
        printf("\nTest to the NandFlash is OK!\n");
    }
   
}

/**************************************/
void Test_RTC( int Is_Test)
{
        // 测试
    RTC_TIME  RTC={0x55,0x12,0x11,0x01,0x22,0x07,0x24};	//初始化时间结构体变量（设置时间：2016年1月26日 14:59:55  星期二）
    //           55秒 47分 17时 周日 28日 4月 24年

    RTC_set_data(RTC);  //设置时间演示
    RTC_TIME TIME_1;
    RTC_read_data(&TIME_1);
    ESP_LOGI(TAG, "20%d/%d/%d %d:%d:%d\n",TIME_1.year,TIME_1.month,TIME_1.day,\
                                            TIME_1.hour,TIME_1.minute,TIME_1.second);
}

/******************************/
void Test_GPIO( int Is_Test) {
    uint32_t a = 0, b = 1;

    gpio_set_level(ASW3401_SEL_N , a);
    gpio_set_level(ASW3401_OE_N , b);

    for (int i = 10000; i >= 0; i --) {
        if (i % 2 == 0) {
            a = 0 ; b = 1;
        } else {
            a = 1 ; b = 0;
        }
        printf("\n+++++++++++++\n");
        gpio_set_level(ASW3401_SEL_N , a);
        gpio_set_level(ASW3401_OE_N , b);;
        
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
   
}


void Init_GPIO() {
    esp_err_t err;
    gpio_config_t gpio = {
        .intr_type    = GPIO_INTR_DISABLE,
        .mode         = GPIO_MODE_DEF_OUTPUT,
        .pin_bit_mask = 1ULL << ASW3401_SEL_N | 1ULL << ASW3401_OE_N,
        .pull_down_en = GPIO_PULLDOWN_DISABLE,
        .pull_up_en   = GPIO_PULLUP_DISABLE 
    };

    err = gpio_config( &gpio );
    if (err != ESP_OK) {
        printf("\ngpio config faild 1.\n");
        return;
    } else printf("gpio config OK1");

    return;   
}

void My_system_init() {

    RTC_Init();
    // Init_GPIO();
    // Flash_Init();
    MyUart_init();
    // Init_NandFlash() ;
    // Empty_File();
}
/************************************************************** */


/****************************************************/
void app_main(void)
{
   My_system_init();
  //  Init_GPIO();
   // Test_GPIO(0);
 //  Test_RJMU401( 1);
  // Test_Flash( 1);
  Test_RTC(1);
  while(1)
  {
    vTaskDelay(10);
  }
}
